package day3

import Utils.SparkUtils
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

object Test4_Join {
  def main(args: Array[String]): Unit = {
    val sc: SparkContext = SparkUtils.getSparkContext(4, "yy")
    val rdd1 = sc.makeRDD(List((1, "a"), (2, "b"), (3, "c"),(4,"d")))
    val rdd2 = sc.makeRDD(List((1, 4), (2, 5), (3, 6)))

    val rdd3 = rdd1.join(rdd2)
    val rdd4 = rdd1.leftOuterJoin(rdd2)

//    rdd3.collect().foreach(v => println(v))
    rdd3.collect().foreach(v => println(v._2._1  + "\t" + v._2._2))

    rdd4.collect().foreach(v => println(v._2._1  + "\t" + v._2._2.getOrElse(0)))  // getOrElse():如果是Some,则返回里面的值，否则返回默认值

    // 全外连接
    rdd1.fullOuterJoin(rdd2)


    sc.stop()
  }
}
